import os, configparser


class Config(object):
    """
    # Config().get_content("user_information")
    配置文件里面的参数
    [dbMysql]
    host = 192.168.1.101
    port = 3306
    user = root
    password = python123
    """

    def __init__(self, config_filename="../config/setting.cnf"):
        self.config_filename = config_filename
        self.cf = configparser.ConfigParser()
        self._read_config()

    def _read_config(self):
        """读取配置文件"""
        file_path = os.path.join(os.path.dirname(__file__), self.config_filename)
        self.cf.read(file_path)

    def get_sections(self):
        self._read_config()  # 重新读取配置
        return self.cf.sections()

    def get_options(self, section):
        self._read_config()  # 重新读取配置
        return self.cf.options(section)

    def get_content(self, section):
        self._read_config()  # 重新读取配置
        result = {}
        for option in self.get_options(section):
            value = self.cf.get(section, option)
            result[option] = int(value) if value.isdigit() else value
        return result
